package net.yeah.mickey_zhouyou.util.fcs;

import java.io.IOException;
import java.io.Reader;

import javax.script.AbstractScriptEngine;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

public class FCSScriptEngineImpl extends AbstractScriptEngine {

	@Override
	public Object eval(String script, ScriptContext context)
			throws ScriptException {
		try {
			FCS fcs = FCS.getInstance(script);
			fcs.eval(context);
		} catch (IOException e) {
			throw new ScriptException(e);
		}
		return null;
	}

	@Override
	public Object eval(Reader reader, ScriptContext context)
			throws ScriptException {
		try {
			FCS fcs = FCS.createInstance(reader);
			fcs.eval(context);
		} catch (IOException e) {
			throw new ScriptException(e);
		}
		return null;
	}

	@Override
	public Bindings createBindings() {
		return new SimpleBindings();
	}

	@Override
	public ScriptEngineFactory getFactory() {
		if (this.factory == null)
			synchronized (this) {
				if (this.factory == null)
					this.factory = new FCSScriptEngineFactory();
			}
		return this.factory;
	}

	private ScriptEngineFactory factory = null;

}
